using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Toenda.Rails.System;
using Toenda.Rails.DataContainer;

public partial class Controls_Main_ctrl_search : System.Web.UI.UserControl {
	private AccountProvider ap = new AccountProvider();
	private StaticProvider sp = new StaticProvider();

	/// <summary>
	/// Page_Load
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e) {
		//bool loggedin = false;
		ArrayList arr;

		if(Session.Count > 0 && ap.CheckSessionExist(Session["Zappr"].ToString())) {
			Account tmp = this.ap.GetAccountBySession(Session["Zappr"].ToString());

			//loggedin = true;
			searchBoxDD.Visible = true;
			searchBoxEmpty.Visible = false;
			panelType.CssClass = "posDdlType";

			if(tmp.Language == "de-DE") {
				arr = sp.GetStaticItems("IT02");
			}
			else {
				arr = sp.GetStaticItems("IT01");
			}

			this.ddlType.DataSource = arr;
			this.ddlType.DataValueField = "Value";
			this.ddlType.DataTextField = "Content";
			this.ddlType.DataBind();

			this.ddlType.Items.Insert(
				0,
				new ListItem(GetLocalResourceObject("AllImages").ToString(), "0")
			);
		}
		else {
			searchBoxDD.Visible = false;
			searchBoxEmpty.Visible = true;

			if(base.Page.Culture == "de-DE") {
				arr = sp.GetStaticItems("IT02");
			}
			else {
				arr = sp.GetStaticItems("IT01");
			}

			this.ddlType.DataSource = arr;
			this.ddlType.DataValueField = "Value";
			this.ddlType.DataTextField = "Content";
			this.ddlType.DataBind();
			
			this.ddlType.Items.Insert(
				0,
				new ListItem(GetLocalResourceObject("AllImages").ToString(), "0")
			);
		}

		this.ddlType.SelectedValue = "0";
	}

	/// <summary>
	/// btnSearch_Click
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnSearch_Click(object sender, EventArgs e) {
		if(Session.Count > 0 && ap.CheckSessionExist(Session["Zappr"].ToString())) {
			switch(this.ddlSearchFor.SelectedValue) {
				case "query":
					Response.Redirect(
						"~/search.aspx?q=" + txtSearch.Text.Trim() 
						+ "&target=a"
						+ "&type=" + ddlType.SelectedValue
					);
					break;

				case "tags":
					Response.Redirect(
						"~/search.aspx?tag=" + txtSearch.Text.Trim() 
						+ "&target=a"
						+ "&type=" + ddlType.SelectedValue
					);
					break;

				case "cam":
					Response.Redirect(
						"~/search.aspx?cam=" + txtSearch.Text.Trim() 
						+ "&target=a"
						+ "&type=" + ddlType.SelectedValue
					);
					break;

				default:
					break;
			}
		}
		else {
			switch(this.ddlSearchFor.SelectedValue) {
				case "query":
					Response.Redirect(
						"~/search.aspx?q=" + txtSearch.Text.Trim() 
						+ "&target=a"
						+ "&type=" + ddlType.SelectedValue
					);
					break;

				case "tags":
					Response.Redirect(
						"~/search.aspx?tag=" + txtSearch.Text.Trim() 
						+ "&target=a"
						+ "&type=" + ddlType.SelectedValue
					);
					break;

				case "cam":
					Response.Redirect(
						"~/search.aspx?cam=" + txtSearch.Text.Trim() 
						+ "&target=a"
						+ "&type=" + ddlType.SelectedValue
					);
					break;

				default:
					break;
			}
		}
	}
}
